<link rel="stylesheet" href="themes/editor/default.css" />
<script charset="utf-8" type="text/javascript" src="js/editor/editor-min.js"></script>
<script charset="utf-8"  type="text/javascript" src="js/editor/editor_zh_CN.js"></script>
<form id="addConsultfm" method="post" novalidate>
<div class="ftitle">添加咨询</div>
	<div id="add_Consult_1" next="#add_Consult_2">
        	<input name="id" type="hidden">
        	<div class="fitem">
                	<label>标题:</label>
                <input name="title"  class="easyui-validatebox" style="width:245px" required="true">
            </div>
        	<div class="fitem">
                	<label>商业机会:</label>
                    <select name="addConsult_opportunity" id="addConsult_opportunity" style="width:150px"  class="easyui-validatebox" required="true">
    				</select>
            </div>

            <div class="fitem">
                	<label>咨询人:</label>
                <input id="consultUser" name="user" style="width:150px">
            </div>
            <div class="fitem">
           			 <label>咨询时间:</label>
           			 <input name="consultTime" style="width:150px" class="easyui-datetimebox" required>
            </div>
            <div class="fitem">
                <label>咨询途径:</label>
                <input id="consultWay" name="consultWay" style="width:150px">
            </div>
            <div class="fitem">
                <label>咨询级别:</label>
                <input id="consultLevel" name="consultLevel" style="width:150px">
            </div>
            <div class="fitem">
                <label>整形项目:</label>
                <input id="customerItems" name="items" style="width:150px">
            </div>
            <div class="tabLink">
				<ul>
					<li><a href="javascript:void(0)" onclick="javascript:addConsult.nextStep('#add_Consult_1')">下一步</a></li>
				</ul>
			</div>
            
    </div>
    <div id="add_Consult_2" style="display:none" pre="#add_Consult_1" next="#add_Consult_3">
           <div class="fitem">
                <label><b>咨询内容:</b></label>
                <br/><br/>
                <textarea style="width:90%;height:300px" name="content"></textarea>
           </div>  
           <div class="tabLink">
				<ul>
					<li><a href="javascript:void(0)" onclick="javascript:addConsult.preStep('#add_Consult_2')">上一步</a></li>
					<li><a href="javascript:void(0)" onclick="javascript:addConsult.nextStep('#add_Consult_2')">下一步</a></li>
				</ul>
			</div>
     </div>   
     <div id="add_Consult_3" style="display:none" pre="#add_Consult_2">    
            <div class="fitem">
                <label><b>咨询分析:</b></label>
                <br/><br/>
                <textarea style="width:90%;height:200px" name="analysis"></textarea>
            </div>  
            <div class="tabLink">
				<ul>
					<li><a href="javascript:void(0)" onclick="javascript:addConsult.preStep('#add_Consult_3')">上一步</a></li>
					<li><a href="javascript:void(0)" onclick="javascript:addConsult.saveConsult()">确定</a></li>
				</ul>
			</div>  
           
                
     </div>                          
        </form>
    
    <div id="CustomerAction" title="添加客户" class="easyui-dialog" style="width:600px;height:500px;padding:10px 20px;"
            closed="true">
    </div>
    
    <div id="toolbar" style="padding:5px;height:auto">
    		按员工过滤:
			<input class="easyui-combobox" id="employeeFilterList" name="employeeFilterList" panelHeight="auto" style="width:100px">

        <div id="searchFilter">
        </div>
    </div>
<script type="text/javascript">
(function( addConsult, $, undefined ) {

	var start = 0;
	var contentK = null;
	var analysisK = null;
	addConsult.init = function ()
	{	
		analysisK = KindEditor.create('textarea[name="analysis"]', {
			resizeType : 1,
			allowPreviewEmoticons : true,
			allowImageUpload : false,
			basePath :"js/editor/",
			themesPath : "themes/editor/",
			themeType : 'default',
			items : [
				'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
				'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
				'insertunorderedlist', '|', 'emoticons', 'image', 'link']
		});
		contentK = KindEditor.create('textarea[name="content"]', {
			resizeType : 1,
			allowImageUpload : true,
			basePath :"js/editor/",
			themesPath : "themes/editor/",
			themeType : 'default',
			uploadJson : 'file/upload',
			filePostName : "cfile",
			allowMediaUpload : true,
			extraFileUploadParams : {
                type : 'consult'
        		},
			items : [
				'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
				'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
				'insertunorderedlist', '|', 'emoticons', 'image', 'link']
		});
		$('#addConsult_opportunity').combogrid({
			    panelWidth:650,
			    panelHeight:250,
			    idField:'id',
			    textField:'name',
			    url:null,
			    columns:[[
							{title:'名称',field:'name',width:150},
			             	{title:'客户信息',field:'customerName',width:120,sortable:true},
			             	{title:'当前阶段',field:'strategy',width:80,sortable:true},
			             	{title:'创建时间',field:'createTime',width:150,sortable:true},
			    ]],
			    pagination:true,
			    pageSize:5,
				pageList:[5],
				toolbar:'#toolbar'
		});

		
		var grid = $('#addConsult_opportunity').combogrid('grid');
		
		$(grid).datagrid('getPager').pagination({   
		    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录',   
		    onSelectPage : function(pPageIndex, pPageSize) {   
		    	
		        var gridOpts = $(grid).datagrid('options');   
		        gridOpts.pageNumber = pPageIndex;   
		        gridOpts.pageSize = pPageSize;     

		        start = (pPageIndex -1)*pPageSize;
		        postSearch();
		    },
		    onChangePageSize : function(pageSize){
		    	countPerPage = pageSize;
		    }
		});

		workbenchJs.loadData("dispatch/customer/opportunity/all?start=0&size=5",addConsult.loadCAData,"查看商业机会");
		workbenchJs.loadData("dispatch/manage/item/all",addConsult.loadItemsResult,"查询整形项目");
		workbenchJs.loadData("dispatch/manage/consultLevel/all",addConsult.loadConsultLevel,"查询咨询级别");
		workbenchJs.loadData("dispatch/manage/consultway/all",addConsult.loadConsultWay,"查询咨询方式");
		workbenchJs.loadData("dispatch/manage/employee/simpleAll",addConsult.loadEmployeeResult,"查看用户列表");
	};

	addConsult.loadCAData = function (result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var payload = jQuery.parseJSON(result.payload.value);
        	var grid = $('#addConsult_opportunity').combogrid('grid');
    		$(grid).datagrid("loaded");
        	$(grid).datagrid('loadData',payload);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	addConsult.loadEmployeeResult = function(result){
		var payload = jQuery.parseJSON(result.payload.value);
		$('#consultUser').combobox({
			url:null,
			valueField:'name',
			textField:'name',
			multiple:false,
			panelHeight:'auto',
			data:payload
		});
		var employeedata = '';
		employeedata = '[{"label":"*","name":"全部"}';
		$(payload).each(function(index){
			var label = payload[index].name;
			employeedata += ',{"label":"'+label+'","name":"'+label+'"}';
		});
		employeedata += ']';
		employeedata = jQuery.parseJSON(employeedata);
		$('#employeeFilterList').combobox({
			url:null,
			valueField:'label',
			textField:'name',
			multiple:false,
			panelHeight:'auto',
			data:employeedata,
			onSelect:addConsult.searchByEmployee
		});
	};
	addConsult.nextStep = function (id){
		if($('#addConsultfm').form('validate'))
		{
			$(id).hide();
			var nextId = $(id).attr('next');
			$(nextId).show();
		}
	};
	addConsult.preStep = function (id){
		$(id).hide();
		var preId = $(id).attr('pre');
		$(preId).show();
	};
	addConsult.CustomerActionCompelete = function (result){
		
		var value;
		if(typeof(result.payload)== 'undefined')
			value  = result;
		else
			value = eval('('+result.payload.value+')');
		
		
		$('#CustomerAction').dialog('close');
		$("input[name='customer']").attr("value",value.id);
		$("#customerName").html(value.name);
		
	};
	function opendiag (url,a){
		$('#CustomerAction').dialog("open");
		$('#CustomerAction').dialog('refresh',url);
		
	};
	
	addConsult.loadItemsResult = function(result){
		var payload = jQuery.parseJSON(result.payload.value);
		$('#customerItems').combobox({
			url:null,
			valueField:'name',
			textField:'name',
			multiple:true,
			panelHeight:'auto',
			data:payload
		});
	};
	addConsult.loadConsultLevel = function(result){
		
		var payload = jQuery.parseJSON(result.payload.value);
		$('#consultLevel').combobox({
			url:null,
			valueField:'name',
			textField:'name',
			multiple:false,
			panelHeight:'auto',
			data:payload
		});
	};
	addConsult.loadConsultWay = function(result){
		
		var payload = jQuery.parseJSON(result.payload.value);
		$('#consultWay').combobox({
			url:null,
			valueField:'name',
			textField:'name',
			multiple:false,
			panelHeight:'auto',
			data:payload
		});
	};
	addConsult.saveConsult = function (){
		var consult = {};
		consult.opportunity = parseInt($("#addConsultfm input[name='addConsult_opportunity']").val());
		consult.user = $("#addConsultfm input[name='user']").val();
		consult.title = $("#addConsultfm input[name='title']").val();
		consult.consultTime = $("#addConsultfm input[name='consultTime']").val();
		consult.consultWay = $("#addConsultfm input[name='consultWay']").val();
		consult.consultLevel = $("#addConsultfm input[name='consultLevel']").val();

		consult.analysis = analysisK.html();
		consult.content = contentK.html();
		
    	var items = "";
    	var r = $('#customerItems').combobox('getValues');
    	$(r).each(function(index){
    		if(items != "")items += ','+r[index];
    		else items = r[index];
    	});
    	consult.items = items;
    	
    	var str = $.toJSON(consult);
    	str = 'parameters='+str;
    	
    	if($('#addConsultfm').form('validate'))
    	{
    		$.ajax({
                type: "post",
                url: "dispatch/customer/consult/add",
                dataType:"json",
                data: str,
                cache: false,
                success: function (result) {
                	var res = result.result;
                    if(res == "SUCCESS")
                    {
                    	$.messager.alert('Success','添加成功','info',function(){
                            workbenchJs.open1('component/customer/consult.html',this);
                        });
                    }else if(res == "COMPONENT_ERROR")
                    {
                    	$.messager.alert("Error",result.payload.value);
                    }else
                    {
                    	$.messager.alert("Error",result.result);
                    }
                    
                }
                
            });
    	}
    };

	var opportunity_employee_search = '';

    addConsult.searchByEmployee = function(){
		opportunity_employee_search = $("input[name='employeeFilterList']").val();
    	var grid = $('#addConsult_opportunity').combogrid('grid');
		$(grid).datagrid("loading");
		//refreshSearchFilter();
		start = 0;
		postSearch();
	}
	addConsult.clearSearch = function()
	{
		countPerPage = 5;
		start = 0;
		opportunity_employee_search = '';
		loadData("dispatch/customer/opportunity/all?start=0&size=5",loadCAData);
	}
	function postSearch()
	{
		var str = "";
		str = appendURL(str,"employee="+opportunity_employee_search);
		str = appendURL(str,'start='+start+'&size=5');
		$.ajax({
            type: "post",
            url: "dispatch/customer/opportunity/all",
            dataType:"json",
            data: str,
            cache: false,
            success: function (result) {
            	
            	addConsult.loadCAData(result);
            }
		});
	}
	function appendURL(url,str1)
	{
		if(typeof(str1) == 'undefined')return url;
		if(url == "" || typeof(url) == 'undefined')url = str1;
		else url+="&"+str1;

		return url;
	}
	
	addConsult.ajaxFileUpload = function(){
        $.ajaxFileUpload(
        {
         	url:'file/upload',
        	secureuri:false,
         	fileElementId:'cfile',
        	dataType: 'json',
         	success: function (data, status)
        	{   
         		$.messager.alert("Success",data.result);
         	},
         	error: function (data, status, e)
       	 	{
         			alert(e);
         		
         		//$('#uploadResult').innerText = data;
//         		$.messager.alert("Error",data.result);
         	}
     	})
       }
}( window.addConsult = window.addConsult || {}, jQuery ));

$(function(){
	addConsult.init();
});
	</script>
    <style type="text/css">
        #fm,addConsultfm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
         .c-label{
            display:inline-block;
            width:150px;
            height:24px;
            padding:0;
            line-height:24px
        }
        
    </style>